package com.mogist.hqc.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mogist.hqc.R;

/**
 * @author tale
 */
public class CustomNumberPick extends LinearLayout
{
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private TextView vTvCount;
	private ImageView vIvReduce, vIvAdd;

	public int mCount = 1, maxNum = 99, minNum = 0, stepNum = 1;

	public int getMaxNum()
	{
		return maxNum;
	}

	public void setMaxNum(int maxNum)
	{
		this.maxNum = maxNum;
		setButtonAble();
	}

	public int getMinNum()
	{
		return minNum;
	}

	public void setMinNum(int minNum)
	{
		this.minNum = minNum;
		setButtonAble();
	}

	public int getStepNum()
	{
		return stepNum;
	}

	public void setStepNum(int stepNum)
	{
		this.stepNum = stepNum;
	}

	public int getCount()
	{
		return mCount;
	}

	public void setCount(int count)
	{
		this.mCount = count;
		vTvCount.setText(String.valueOf(mCount));
		setButtonAble();
	}

	public void setAddClickListener(OnClickListener ock)
	{
		vIvAdd.setOnClickListener(ock);
	}

	public void setCountClickListener(OnClickListener listener)
	{
		vTvCount.setOnClickListener(listener);
	}

	public void setReduceClickListener(OnClickListener ock)
	{
		vIvReduce.setOnClickListener(ock);
	}

	/**
	 * 数量选择 控件
	 *
	 * @param context
	 * @param attrs
	 */
	public CustomNumberPick(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		mContext = context;
		initView(R.layout.widget_customnumpick, attrs);
	}

	private void initView(int menuViewID, AttributeSet attrs)
	{
		mLayoutInflater = LayoutInflater.from(mContext);
		final View sub_view = mLayoutInflater.inflate(menuViewID, this);
		vIvReduce = (ImageView) sub_view.findViewById(R.id.vIvReduce);
		vIvAdd = (ImageView) sub_view.findViewById(R.id.vIvAdd);
		vTvCount = (TextView) sub_view.findViewById(R.id.vTvCount);
		vTvCount.setText(String.valueOf(mCount));
		vIvAdd.setOnClickListener(mOclickListener);
		vIvReduce.setOnClickListener(mOclickListener);
		setButtonAble();
	}

	/**
	 * 添加数量
	 */
	public void addCount()
	{
		if(vIvAdd.isEnabled())
		{
			changeEtnum(stepNum);
		}
	}

	/**
	 * 减少数量
	 */
	public void reduceCount()
	{
		if(vIvReduce.isEnabled())
		{
			changeEtnum(-stepNum);
		}
	}

	@Override public void setEnabled(boolean flag)
	{
		vIvReduce.setEnabled(flag);
		vIvAdd.setEnabled(flag);
	}

	/**
	 * 该表数量
	 *
	 * @param changValue 需要改变数量的值
	 */
	private void changeEtnum(int changValue)
	{
		setCount(mCount+changValue);
	}

	/**
	 * 设置改变数量按钮可否点击
	 */
	private void setButtonAble()
	{
		if(mCount > minNum)
		{
			vIvReduce.setEnabled(true);
		}
		else
		{
			vIvReduce.setEnabled(false);
		}
		if(mCount < maxNum)
		{
			vIvAdd.setEnabled(true);
		}
		else
		{
			vIvAdd.setEnabled(false);
		}
	}

	private OnClickListener mOclickListener = new OnClickListener()
	{

		@Override public void onClick(View v)
		{
			int i = v.getId();
			if(i == R.id.vIvReduce)
			{
				reduceCount();
			}
			else if(i == R.id.vIvAdd)
			{
				addCount();
			}
			else
			{
			}
		}
	};

	// --------------- 获取自定义子View ---------------

	public TextView getTvCount()
	{
		return vTvCount;
	}

	public ImageView getIvReduce()
	{
		return vIvReduce;
	}

	public ImageView getIvAdd()
	{
		return vIvAdd;
	}
}
